import cv2
import numpy as np
if __name__ == '__main__':
    # 1. 图片输入
    image_np = cv2.imread("C:\\Users\\20436\Desktop\\liumangxing.jpg")

    # 2. 灰度化
    image_np_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)

    # 3. 二值化
    _, image_np_thresh = cv2.threshold(
        image_np_gray,
        74,
        255,
        cv2.THRESH_BINARY
    )
    # 4.高斯滤波
    no_noise_image = cv2.GaussianBlur(
        image_np_thresh,  # 要滤波的图像
        (3, 3),  # 核
        1  # SigmaX
    )

    # 5. 查找轮廓
    contours, hierarchy = cv2.findContours(
        no_noise_image,
        cv2.RETR_CCOMP,
        cv2.CHAIN_APPROX_SIMPLE
    )

    image = cv2.drawContours(
        image_np,
        contours[1:],
        contourIdx=-1,
        color=(255, 234, 0),
        thickness=2
    )
    cv2.imshow('contour_image', image)
    cv2.imwrite('contour_image.jpg',image)
    cv2.waitKey(0)

